package com.compomics.prioritizer.db.accessors;

import java.util.HashMap;
import java.util.Vector;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * Created by IntelliJ IDEA.
 * User: niklaas
 * Date: 16-dec-2008
 * Time: 13:22:59
 * To change this template use File | Settings | File Templates.
 */
public class Log extends LogTableAccessor {

    /**
     * Simple wrapper for the superclass constructor.
     *
     * @param aParams   HashMap with the parameters.
     */
    public Log(HashMap aParams) {
        super(aParams);
    }


    /**
     * This constructor reads a Log from a ResultSet. The ResultSet should be positioned such that
     * a single row can be read directly (i.e., without calling the 'next()' method on the ResultSet). <br />
     * The columns should be in this order: <br />
     * Column 1: Log ID <br />
     * Column 2: name <br />
     * Column 3: e_mail <br />
     * Column 4: role <br />
     * Column 5: Logname <br />
     * Column 6: creationdate <br />
     * Column 7: modificationdate.
     *
     * @param   aRS ResultSet to read the data from.
     * @exception   java.sql.SQLException    when reading the ResultSet failed.
     */
    public Log(ResultSet aRS) throws SQLException {
        this.iLogid = aRS.getLong(1);
        this.iL_projectid = aRS.getLong(2);
        this.iStatus = aRS.getString(3);
        this.iUsername = aRS.getString(4);
        iCreationdate = (java.sql.Timestamp)aRS.getObject(5);
        iModificationdate = (java.sql.Timestamp)aRS.getObject(6);
    }

    /**
     * This method retrieves all Logs from the connection and stores them in a HashMap. <br />
     * The Logid is the key (Long type) and the Log object is the value.
     *
     * @param aConn Connection to retrieve the Logs from.
     * @return  HashMap with the Logs, Logid is the key (Long type) and Log objects are the values.
     * @throws SQLException when the retrieve failed.
     */
    public static HashMap getAllLogsAsMap(Connection aConn) throws SQLException {
        HashMap lLogs = new HashMap();
        PreparedStatement prep = aConn.prepareStatement("select * from log");
        ResultSet rs = prep.executeQuery();
        while(rs.next()) {
            Log temp = new Log(rs);
            lLogs.put(new Long(temp.getLogid()),temp);
        }
        rs.close();
        prep.close();

        return lLogs;
    }

    /**
     * This method retrieves all Logs from the connection and stores them in a Log[].
     *
     * @param aConn Connection to retrieve the Logs from.
     * @return  Log[] with the Logs.
     * @throws SQLException when the retrieve failed.
     */
    public static Log[] getAllLogs(Connection aConn) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from log");
        ResultSet rs = prep.executeQuery();
        Vector temp = new Vector();
        while(rs.next()) {
            temp.add(new Log(rs));
        }
        Log[] result = new Log[temp.size()];
        temp.toArray(result);
        rs.close();
        prep.close();
        return result;
    }

     /**
     * This methods reads the newest Logs from the Log table
     *
     * @param aConn Connection to read the Logs from.
     * @param aId Connection to read the Logs from.
     * @return  Log with the Log in the 'Log' table.
     * @throws SQLException when the retrieving of the Log went wrong.
     */
    public static Log getNewestLogForProject(Connection aConn, Long aId) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from log where l_projectid = ? order by creationdate desc");
        prep.setLong(1, aId);
        ResultSet rs = prep.executeQuery();
        Log lLog = null;
        rs.next();
        lLog = new Log(rs);

        rs.close();
        prep.close();

        return lLog;
    }

    /**
     * This methods reads the all Logs from the Log table for a specific project
     *
     * @param aConn Connection to read the Logs from.
     * @param aId Project id
     * @return  Log with the Log in the 'Log' table.
     * @throws SQLException when the retrieving of the Log went wrong.
     */
    public static Log[] getAllLogsForProject(Connection aConn, Long aId) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from log where l_projectid = ? order by creationdate asc");
        prep.setLong(1, aId);
        ResultSet rs = prep.executeQuery();
        Vector<Log> lLogsVector = new Vector<Log>();
        while(rs.next()){
            lLogsVector.add(new Log(rs));
        }

        Log[] lLogs = new Log[lLogsVector.size()];
        lLogsVector.toArray(lLogs);
        rs.close();
        prep.close();

        return lLogs;
    }


    /**
     * This methods reads all Logs from the Log table
     *
     * @param aConn Connection to read the Logs from.
     * @param aId Connection to read the Logs from.
     * @return  Log with the Log in the 'Log' table.
     * @throws SQLException when the retrieving of the Log went wrong.
     */
    public static Log getLogById(Connection aConn, Long aId) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from log where logid = ?");
        prep.setLong(1, aId);
        ResultSet rs = prep.executeQuery();
        Log lLog = null;
        while(rs.next()) {
            lLog = new Log(rs);
        }
        rs.close();
        prep.close();

        return lLog;
    }


    /**
     * This method returns a String representation of the Log, ie.: the name.
     *
     * @return  String  with the name of the Log.
     */
    public String toString() {
        return this.iStatus;
    }
}


